package org.alex.samples.web;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.alex.samples.model.User;
import org.alex.samples.model.UserVO;
import org.alex.samples.service.IUserService;
import org.alex.samples.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class LoginController {

	@Autowired
	private IUserService userService;

	// 进入登录界面
	@RequestMapping(value = { "/login" }, method = RequestMethod.GET)
	public String toLogin(Model model) {
		model.addAttribute("vo", new UserVO());
		return "login/login";
	}

	// 判断账号密码
	@RequestMapping(value = { "/login" }, method = RequestMethod.POST)
	public String doLogin(UserVO vo, HttpSession session, RedirectAttributes redirectAttributes) throws Exception {
		if (vo != null) {
			UserVO result = userService.getUserById(vo.getId());
			if (result != null) {
				if (vo.getPassword() != null) {
					String MD5Password = MD5Util.toMD5(vo.getPassword());
					Date currentTime = null; //
					Calendar currentTimeCalendar = Calendar.getInstance(); // 创建日历对象
					currentTimeCalendar.setTime(new Date()); // 初始化时间
					currentTimeCalendar.add(Calendar.MINUTE, -5); // 减5分钟
					currentTime = currentTimeCalendar.getTime();
					if (result.getLastLockTime().before(currentTime)) { // before:date1小于date2时返回true,反之false
						if (MD5Password.equals(result.getPassword())) {
							// 登录成功时清空
							result.setLoginFailCount(0);
							userService.updataUserLoginFailCount(result);
							session.setAttribute("user", result);
							return "redirect:/toIndex";
						} else {
							result.setLoginFailCount(result.getLoginFailCount() + 1);
							if (result.getLoginFailCount() == 3) {
								result.setLoginFailCount(0);
								result.setLastLockTime(new Date());
								userService.updataUserLastLockTime(result);
							}
							userService.updataUserLoginFailCount(result);
							redirectAttributes.addFlashAttribute("passwordMessage", "密码错误");
						}
					} else {
						// 再锁定范围内
						redirectAttributes.addFlashAttribute("message", "该账号已被锁定，请稍等五分钟后再登录。");
					}
				}
			} else {
				redirectAttributes.addFlashAttribute("userNameMessage", "该用户不存在");
			}
		}
		return "redirect:/login";
	}

	// 登出
	@RequestMapping(value = { "/logout" }, method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:/login";
	}

	// 进入首页
	@RequestMapping(value = { "/toIndex" }, method = RequestMethod.GET)
	public String toIndex() {
		return "public/index";
	}

}
